/**
* 系统管理 角色管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">

      <el-form-item label="角色名称：">
        <el-input size="small" v-model="formInline.roleName" placeholder="输入角色名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border
      element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column sortable prop="id" label="id" width="300">
      </el-table-column>
      <el-table-column sortable prop="roleName" label="角色名称" width="300">
      </el-table-column>
      <el-table-column sortable prop="roleCode" label="角色代码" width="300">
      </el-table-column>
      <el-table-column sortable prop="createTime" label="创建时间" width="300">
        <template slot-scope="scope">
          <div>{{scope.row.createTime}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="deleteById(scope.row)">删除</el-button>
          <el-button size="mini" type="success" @click="getUmsMenu(scope.row)">菜单权限</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog title="添加" :visible.sync="editFormVisible" width="30%" @click='closeDialog("edit")'>
      <el-form label-width="120px" :model="editForm" ref="editForm" :rules="rules">
        <el-form-item label="角色编码" prop="roleCode">
          <el-input size="small" v-model="editForm.roleCode" auto-complete="off" placeholder="请输入系统编码"></el-input>
        </el-form-item>
        <el-form-item label="角色名称" prop="roleName">
          <el-input size="small" v-model="editForm.roleName" auto-complete="off" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input size="small" v-model="editForm.remark" auto-complete="off" placeholder="请输入角色代码"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存
        </el-button>
      </div>
    </el-dialog>
    <!-- 菜单权限 -->
    <el-dialog title="菜单权限" :visible.sync="menuAccessshow" width="30%" @click='closeDialog("perm")'>
      <el-tree :data="menuData" ref="tree" show-checkbox node-key="id" :default-checked-keys="defaultCheckedKeys"
        check-strictly default-expand-all :props="defaultProps" :label="menuName">
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("perm")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="menuPermSave">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    roleList,
    roleSave,
    roleDelete,
    rolePwd,
    RoleRightTree,
    RoleRightSave
  } from '../../api/userMG'
  import Pagination from '../../components/Pagination'
  export default {
    data() {
      return {
        loading: false, //是显示加载
        editFormVisible: false, //控制编辑页面显示与隐藏
        menuAccessshow: false, //控制数据权限显示与隐藏
        editForm: {
          id: '',
          roleCode: '',
          roleName: '',
          remark: '',
        },
        // rules 表单验证
        rules: {
          roleCode: [{
            required: true,
            message: '请输入角色编码',
            trigger: 'blur'
          }],
          roleName: [{
            required: true,
            message: '请输入角色名称',
            trigger: 'blur'
          }],
        },
        formInline: {
          page: 1,
          rows: 10,
          roleName: ''
        },
        listData: [], //用户数据

        // 分页参数
        pageparm: {
          currentPage: 1,
          pageSize: 10,
          total: 10
        },
        //菜单权限
        menuData: [],
        defaultProps: {
          children: 'menus',
          label: 'menuName'
        },
        menuName: '',
        //默认选中的数组id
        defaultCheckedKeys: [],
        roleId: '',
      }
    },
    // 注册组件
    components: {
      Pagination
    },

    /**
     * 创建完毕
     */

    created() {
      this.getdata()
    },

    /**
     * 里面的方法只有被调用才会执行
     */

    methods: {
      // 获取角色列表
      getdata() {
        this.$http({
          url: "sys/umsRole/findUmsRolePageByParam",
          method: 'GET',
          params: this.formInline
        }).then(res => {
          console.log(res)
          if (res.code == 200) {
            this.loading = false
            this.listData = res.data.list
            this.pageparm.currentPage = res.data.pageNum
            this.pageparm.pageSize = res.data.pageSize
            this.pageparm.total = res.data.total
            console.log(123, this.pageparm)
          } else {
            this.loading = false
          }
        })
      },
      //获取菜单
      getUmsMenu(row) {
        this.roleId = row.id
        this.$http({
          url: "sys/umsMenu/findUmsMenusTree",
          method: 'GET',
          params: {
            roleId: this.roleId
          }
        }).then(res => {
          console.log(res)
          this.menuAccessshow = true
          if (res.code == 200) {
            this.menuData = res.data.umsMenus
            this.defaultCheckedKeys = res.data.collect
            this.menuData.forEach((item) => {
              root.$nextTick(() => {
                let node = refs.tree.getNode(item.moduleNo);
                if (node.isLeaf) { //判断是否是子节点
                  refs.tree.setChecked(node, true);
                }
              })
            });

            console.log('选中数组', this.defaultCheckedKeys)
          }
        })
      },
      // 分页插件事件
      callFather(parm) {
        this.formInline.page = parm.currentPage
        this.formInline.limit = parm.pageSize
        this.getdata(this.formInline)
      },
      // 搜索事件
      search() {
        this.formInline.page = 1
        this.getdata(this.formInline)
      },
      //显示添加界面
      handleEdit: function(index, row) {
        this.editFormVisible = true
        this.editForm.id = '';
        this.editForm.roleCode = '';
        this.editForm.roleName = '';
        this.editForm.remark = '';
      },
      // 编辑、增加页面保存方法
      submitForm(editData) {
        this.$refs[editData].validate(valid => {
          if (valid) {
            this.$http({
              url: 'sys/umsRole/insert',
              method: 'POST',
              data: this.editForm
            }).then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.code == 200) {
                this.getdata()
              }
            }).catch(res => {
              console.log(res)
              this.$message.error(res.message)
            })
          } else {
            return false
          }
        })
      },
      // 删除角色
      deleteById(row) {
        console.log(row)
        this.$confirm('确定要删除吗?', '信息', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(() => {
            this.$http({
              url: "sys/umsRole/deteleById/" + row.id,
              method: 'GET',
            }).then(res => {
              if (res.code == 200) {
                this.getdata()
              }
            })
          })
          .catch((e) => {
            console.log(e)
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      },

      // 菜单权限-保存
      menuPermSave() {
        var res = this.$refs.tree.getCheckedNodes()
        console.log(res)
        let arr = []
        res.forEach((item) => {
          arr.push(item.id)
        })
        let menuids = arr.join(',');
        var param = {
          roleId: this.roleId,
          menuIds: menuids
        }
        console.log(11, param)
        this.$http({
          url: "sys/roleMenu/distribution",
          method: 'GET',
          params: param
        }).then(res => {
          console.log(res)
          if (res.code == 200) {
            this.menuAccessshow = false
          }
        })

        console.log(childsIds)

      },
      // 关闭编辑、增加弹出框
      closeDialog(dialog) {
        if (dialog == 'edit') {
          this.editFormVisible = false
        } else if (dialog == 'perm') {
          this.menuAccessshow = false
        }
      }
    }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }

  .userRole {
    width: 100%;
  }
</style>
